﻿using System;
using System.ServiceModel.Activation;

namespace SimpleWcf
{
    /// <summary>
    /// An extended service route with extra properties
    /// </summary>
    public class SimpleServiceRoute : ServiceRoute
    {
        private readonly Type serviceType;

        private SimpleServiceRoute(string routePrefix, ServiceHostFactoryBase serviceHostFactory, Type serviceType) 
            : base(routePrefix, serviceHostFactory, serviceType)
        {
            ServicePath = routePrefix;
            this.serviceType = serviceType;
        }

        /// <summary>
        /// The path to the service.  Ex: /Service1.svc
        /// </summary>
        public string ServicePath { get; private set; }

        /// <summary>
        /// The service's Type
        /// </summary>
        public Type ServiceType { get { return serviceType; } }

        /// <summary>
        /// The description of the service.  Used in the service listing page for the description.  Add [Description("Blah blah")] to your service.
        /// </summary>
        public string ServiceDescription
        {
            get
            {
                var descriptionAttributes = ServiceType.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), true);
                return descriptionAttributes.Length == 0 
                    ? null
                    : ((System.ComponentModel.DescriptionAttribute)descriptionAttributes[0]).Description;
            }
        }

        /// <summary>
        /// Creates the Service route just using the route and type.
        /// </summary>
        /// <param name="route"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static SimpleServiceRoute Create(string route, Type type)
        {
            return new SimpleServiceRoute(route, new CustomServiceHostFactory(), type);
        }
    }
}